package com.wiscamp.ninechapters.configuration;


import com.wiscamp.ninechapters.panels.security.JwtAuthenticationFilter;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SpringSecurityConfig {

    private final JwtAuthenticationFilter jwtFilter;

    private final CustomAccessDeniedHandler customAccessDeniedHandler;

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
/*        http
                .csrf().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeHttpRequests()
                *//*.requestMatchers("/api/ControlPanels/Authenticate").permitAll()
                .requestMatchers("/docs/**").permitAll()
                .requestMatchers("/swagger").permitAll()
                .requestMatchers("/swagger-ui/**").permitAll()
                .requestMatchers("/api/ControlPanels/**").hasAuthority("ADMIN")
                .requestMatchers("/api/ControlPanels/Applications/1/Parameters/Code/**").permitAll()
                .requestMatchers("/api/Problems/**").hasAuthority("ADMIN")
                .requestMatchers("/api/Tags/**").hasAuthority("ADMIN")
                .requestMatchers("/user/**").permitAll()
                .requestMatchers("/api/ControlPanels/Applications/**").permitAll()
                .requestMatchers("/superadmin/**").hasAuthority("SUPERADMIN");*//*
                .requestMatchers("/docs/**").permitAll()
                .requestMatchers("/swagger").permitAll()
                .requestMatchers("/swagger-ui/**").permitAll()
                .requestMatchers("/api/**").permitAll();
        http.addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class);*/


        http
                .csrf().disable()
                .cors().and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeHttpRequests()
                .requestMatchers("/api/ControlPanels/Authenticate").permitAll()
                .requestMatchers("/api/ControlPanels/SignUp").permitAll()
                .requestMatchers("/api/ControlPanels/Password/Forgot").permitAll()
                .requestMatchers("/api/ControlPanels/**").hasAuthority("ADMIN")
                .requestMatchers("/api/Problems/**").hasAuthority("ADMIN")
                .requestMatchers("/api/Tags/**").hasAuthority("ADMIN")
                .requestMatchers("/api/Common/Files").hasAuthority("ADMIN")
                .requestMatchers("/docs/**").permitAll()
                .requestMatchers("/swagger").permitAll()
                .requestMatchers("/swagger-ui/**").permitAll();

        http.addFilterAfter(jwtFilter, UsernamePasswordAuthenticationFilter.class);
        http.exceptionHandling(except->except.authenticationEntryPoint(customAccessDeniedHandler));
        return http.build();
    }

    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
        return authenticationConfiguration.getAuthenticationManager();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

}
